#!/bin/bash
. ../lib/sh/ctcslib.sh


if [ -z "$1" ] ; then
	dir=/tmp/linux
else
	dir="$1"
fi


function interrupt () {
	trap interrupt SIGUSR1
        trap interrupt SIGTERM
	trap interrupt SIGINT
#        echo $failed fail $succeed succeed $count count
	rm -f $ls1 $ls2
	rm -rf $dir
        # this just grabs the process id and all children.  and kills them
	# until they're dead
	x=1
	while [ $x -gt 0 ] ; do {
		kill_children --KILL
		x=$?
	} ; done
        exit 255
}
trap interrupt SIGUSR1
trap interrupt SIGTERM
trap interrupt SIGINT

if [ -d "/usr/src/linux" ] ; then
        LINUXDIR=/usr/src/linux
else 
        if [ -d "/usr/src/linux-2.4" ] ; then
        LINUXDIR=/usr/src/linux-2.4
        else
                if [ -d "/usr/src/linux-2.6" ] ; then
                LINUXDIR=/usr/src/linux-2.6
                else
                echo Linux source dir does not exist.  Help'!'
                echo "Tried: /usr/src/linux /usr/src/linux-2.4 /usr/src/linux-2.6"
                exit 2
                fi
        fi
fi

rm -rf $dir >&2
sync
#make sure that the path exists, but not the directory
mkdir -p $dir >&2

cd $LINUXDIR
tar -chf - . |tar -xhf - -C $dir  
cd $dir 

if [ ! -d "$dir" ] ; then {
	echo $dir
	echo test configuration error >&2
	exit 2
} ; fi

echo $1
cd $dir
